<?php
class Index extends Action {
	private $limit = 10;
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$data = array ();
		$start = rqst ( 'start', 1 ); // 分页
		$where = Model::where_build ( array ('gid','type','gname like','name like' ), $data, array ('gname' => 'like','name' => 'like' ) );
		
		$groupModel = new CoreGroupModel ();
		
		$groupModel->sort ( array ('gid','d' ) );
		
		$users = $groupModel->where ( $where )->limit ( $this->limit, $start - 1 )->count ( true )->retrieve ();
		
		$data ['limit'] = $this->limit;
		
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		
		if ($users) {
			$data ['items'] = $users;
			$data ['countTotal'] = $users->countTotal;
		}
		$data ['reserves'] = array (0 => '','1' => '<span class="label">内置</span>' );
		$data ['group_types'] = apply_filter('get_group_types', array(''=>'-请选择组类别-'));
		bind ( 'get_group_options', array ($this,'get_options' ), 10, 2 );
		bind ( 'get_group_bench_options', array ($this,'get_bench_options' ) );
		return new SmartyView ( $data, 'group/list' );
	}
	
	public function get_options($options, $item) {
		$options .= '<a title="用户列表" href="../users/?gid=' . $item ['gid'] . '"><i class="icon-list-alt"></i></a>';
		$options .= '<a title="编辑" href="./?Ctlr=EditGroup&gid=' . $item ['gid'] . '"><i class="icon-edit"></i></a>';
		if (icando ( 'grant', 'user' )) {
			$options .= '<a title="授权" class="grant-group" data-content="' . $item ['gname'] . '" id="grant-GROUP-' . $item ['gid'] . '" href="' . ADMIN_URL . 'dashboard/?Ctlr=Grant&type=GROUP&uid=' . $item ['gid'] . '"><i class="icon-user"></i></a>';
		}
		if (! $item ['reserved'] && icando ( 'delgroup', 'user' )) {
			$options .= '<a title="删除" onclick="return confirm(\'确定要删除该用户组?\');" href="./?Ctlr=DelGroup&gid=' . $item ['gid'] . '"><i class="icon-trash"></i></a>';
		}
		return $options;
	}
	public function get_bench_options($options) {
		if(icando('delgroup','user')){
			$options .= '<li><a href="./?Ctlr=DelGroup" id="menu-del-group"><i class="icon-trash"></i>删除</a></li>';
		}
		return $options;
	}
}